package com.edu.study.base.StuCommonClassifyCatalog.controller;

import java.util.ArrayList;
import java.util.List;

import com.edu.study.base.StuCommonClassifyCatalog.domain.StuCommonClassifyCatalog;
import com.edu.study.base.StuCommonClassifyCatalog.service.IStuCommonClassifyCatalogService;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品组件目录Controller
 * 
 * @author zw
 * @date 2021-05-11
 */
@RestController
@RequestMapping("/stuCommonClassifyCatalog")
public class StuCommonClassifyCatalogController extends BaseController
{
    @Autowired
    private IStuCommonClassifyCatalogService stuCommonClassifyCatalogService;

    /**
     * 查询产品组件目录列表
     */
    @PreAuthorize("@ss.hasPermi('StuCommonClassifyCatalog:StuCommonClassifyCatalog:list')")
    @GetMapping("/list")
    public TableDataInfo list(StuCommonClassifyCatalog stuCommonClassifyCatalog)
    {
        startPage();
        List<StuCommonClassifyCatalog> list = stuCommonClassifyCatalogService.selectStuCommonClassifyCatalogList(stuCommonClassifyCatalog);
        return getDataTable(list);
    }
    List<StuCommonClassifyCatalog> catalogList = null;
    @GetMapping("/treeSelect")
    public AjaxResult getTree(StuCommonClassifyCatalog stuCommonClassifyCatalog){
        List<StuCommonClassifyCatalog> classifyCatalogList = stuCommonClassifyCatalogService.selectStuCommonClassifyCatalogList(stuCommonClassifyCatalog);
        catalogList = new ArrayList<>();
        getChildren(classifyCatalogList);
        List<StuCommonClassifyCatalog> list = stuCommonClassifyCatalogService.buildClassifyCatalogTree(catalogList,stuCommonClassifyCatalog);
        return AjaxResult.success(list);
    }

    /**
     * 获取子级
     * @param classifyCatalogList
     */
    public void getChildren(List<StuCommonClassifyCatalog> classifyCatalogList){
        if(classifyCatalogList!=null && classifyCatalogList.size()>0){
            for(StuCommonClassifyCatalog catalog : classifyCatalogList){
                catalogList.add(catalog);
                StuCommonClassifyCatalog commonClassifyCatalog = new StuCommonClassifyCatalog();
                commonClassifyCatalog.setCatalogParentId(catalog.getId());
                List<StuCommonClassifyCatalog> list = stuCommonClassifyCatalogService.selectStuCommonClassifyCatalogList(commonClassifyCatalog);
                getChildren(list);
            }
        }
    }

    /**
     * 导出产品组件目录列表
     */
    @PreAuthorize("@ss.hasPermi('StuCommonClassifyCatalog:StuCommonClassifyCatalog:export')")
    @Log(title = "产品组件目录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StuCommonClassifyCatalog stuCommonClassifyCatalog)
    {
        List<StuCommonClassifyCatalog> list = stuCommonClassifyCatalogService.selectStuCommonClassifyCatalogList(stuCommonClassifyCatalog);
        ExcelUtil<StuCommonClassifyCatalog> util = new ExcelUtil<StuCommonClassifyCatalog>(StuCommonClassifyCatalog.class);
        return util.exportExcel(list, "StuCommonClassifyCatalog");
    }

    /**
     * 获取产品组件目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('StuCommonClassifyCatalog:StuCommonClassifyCatalog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(stuCommonClassifyCatalogService.selectStuCommonClassifyCatalogById(id));
    }

    /**
     * 新增产品组件目录
     */
    @PreAuthorize("@ss.hasPermi('StuCommonClassifyCatalog:StuCommonClassifyCatalog:add')")
    @Log(title = "产品组件目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StuCommonClassifyCatalog stuCommonClassifyCatalog)
    {
        stuCommonClassifyCatalog = (StuCommonClassifyCatalog)BeanUtils.initBeanPublicProperties(stuCommonClassifyCatalog,"add");
        return toAjax(stuCommonClassifyCatalogService.insertStuCommonClassifyCatalog(stuCommonClassifyCatalog));
    }

    /**
     * 修改产品组件目录
     */
    @PreAuthorize("@ss.hasPermi('StuCommonClassifyCatalog:StuCommonClassifyCatalog:edit')")
    @Log(title = "产品组件目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StuCommonClassifyCatalog stuCommonClassifyCatalog)
    {
        stuCommonClassifyCatalog = (StuCommonClassifyCatalog)BeanUtils.initBeanPublicProperties(stuCommonClassifyCatalog,"update");
        return toAjax(stuCommonClassifyCatalogService.updateStuCommonClassifyCatalog(stuCommonClassifyCatalog));
    }

    /**
     * 删除产品组件目录
     */
    @PreAuthorize("@ss.hasPermi('StuCommonClassifyCatalog:StuCommonClassifyCatalog:remove')")
    @Log(title = "产品组件目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(stuCommonClassifyCatalogService.deleteStuCommonClassifyCatalogByIds(ids));
    }
}
